﻿package{
	import flash.utils.*;
	import MyGeom;
	import com.adobe.serialization.json.JSON;
	public class helper{
		static function clone(source:Object):* {
			var copier:ByteArray = new ByteArray();
			copier.writeObject(source);
			copier.position = 0;
			return(copier.readObject());
		}
		/*
		static function decode(str:String):Array{
			const s:String = str;
			var n:int = 1,Int:String;
			var c:String = s.charAt(1);
			const parent:Array = [];
			var now:Array;
			while(true){
				if(c == ','){
					c = s.charAt(++n);
				}else if(c >= '0' && c <= '9'){
					Int = c;
					while(true){
						c = s.charAt(++n);
						if(c >= '0' && c <= '9'){
							Int += c;
						}else{
							now.push(int(Int));
							break;
						}
					}
				}else if(c == '['){
					now = [];
					parent.push(now);
					c = s.charAt(++n);
				}else if(c == ']'){
					now = parent;
					c = s.charAt(++n);
				}
				else break;
			}
			return parent;
		}*/
		static function decode(str:String):Array{
			var data:Array = JSON.decode(str);
			var result:Array = []
			var i:int,j:int,col:int,row:int;
			var size:int = 200
			for(j = 0;j < 6;j++){
				result.push([]);
				for(i = 0;i < 8;i++){
					result[j].push([]);
				}
			}
			for(i = 0;i < data.length;i++){
				var obj:Array = data[i]
				col = int(obj[0]/size)
				row = int(obj[1]/size)
				var T = new Triangle(obj)
				result[row][col].push(T);
				if(col > 0)result[row][col-1].push(T)
				if(col < 7)result[row][col+1].push(T)
				if(row > 0)result[row-1][col].push(T)
				if(row < 5)result[row+1][col].push(T)
				if(col > 0 && row > 0)result[row-1][col-1].push(T)
				if(col < 7 && row < 5)result[row+1][col+1].push(T)
				if(col > 0 && row < 5)result[row+1][col-1].push(T)
				if(col < 7 && row > 0)result[row-1][col+1].push(T)
			}
			return result;
		}
		static function traceByte(data:ByteArray){
			data.position = 0;
			var txt:Array = [];
			for(var i:int = 0;i < data.length;i++){
				txt.push(data.readUnsignedByte());
			}
			trace(txt);
		}
		static function toArray(data){
			if(data is String){
				var arr:Array = [];
				for(var i:int = 0;i < data.length;i++){
					arr.push(data.charCodeAt(i));
				}
				return arr;
			}
			return data;
		}
	}
}
class Triangle{
	public var x0:Number,y0:Number,x1:Number,y1:Number,x2:Number,y2:Number
	public var x:Number,y:Number;
	function Triangle(source){
		x0 = source[2];y0 = source[3];
		x1 = source[4];y1 = source[5];
		x2 = source[6];y2 = source[7];
	}
}